package com.sys.manage.service;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.sys.api.Service.ManagerService;
import com.sys.common.ZuelResult;
import com.sys.manage.dao.UserDao;
import com.sys.pojo.OrderUser;
import com.sys.vo.GetAllUsersVo;


@Service
@CacheConfig(cacheNames="manager:users")
public class ManagerServiceImpl implements ManagerService {

	@Autowired
    private RedisTemplate<String, Object> redisTemplate;
	
	@Autowired
	private UserDao dao;
	
	@Override
	public ZuelResult changeState(Integer id) {
		// TODO Auto-generated method stub
		Optional<OrderUser> optional = dao.findById(id);
		OrderUser user = optional.get();
		if (user==null) {
			return ZuelResult.error(null, "没有此id的用户");
		}
		if (user.getState().equals("0")) {
			user.setState("1");
		}else {
			user.setState("0");
		}
		dao.save(user);
		redisTemplate.delete("manager:users::getAllUser()");
		return ZuelResult.ok(null, "更改状态成功");
	}

	@Override
	@Cacheable(key="'getAllUser()'")
	public GetAllUsersVo getAllUser() {
		// TODO Auto-generated method stub
		GetAllUsersVo vo = new GetAllUsersVo();
		vo.setList(dao.findAll());
		for (int i = 0; i < vo.getList().size(); i++) {
			if (vo.getList().get(i).getState().equals("0")) {
				vo.getList().get(i).setState("已冻结");
			}
			if (vo.getList().get(i).getState().equals("1")) {
				vo.getList().get(i).setState("已激活");
			}
		}
		return vo;
	}

}
